//
//  SYTongJiListView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/25.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYTongJiListView.h"
#import "SYTongJiTableViewCell.h"



@interface SYTongJiListView () <UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *curTableView;
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (assign, nonatomic) NSInteger index;
@property (strong, nonatomic) SYNoLabel *nolab;


@end


@implementation SYTongJiListView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    self.index = 1;
    
    _dataListArr = [NSMutableArray array];
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(self);
        __weak typeof(self) weakSelf = self;
        [self.curTableView headerRefreshBlock:^{
            [weakSelf.dataListArr removeAllObjects];
            weakSelf.index = 1;
            
            [weakSelf rquestDataList];
        }];
        
        [self.curTableView fooderRefreshBlock:^{
//            if (weakSelf.index == 1) {
//                weakSelf.index = weakSelf.dataListArr.count/10 +2;
//            }else{
//                weakSelf.index = weakSelf.dataListArr.count/10 +1;
//            }
//            weakSelf.index ++;
            [weakSelf rquestDataList];
        }];
    }];
    self.nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
}

- (void)rquestDataList {
    
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestStatisticsServiceExamListPageIndex:self.index pageSize:10 success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        [self.curTableView sy_endRefreshing];
        if (dataListArr.count <=0) {
        }else{
            self->_index++;
        }
        [self.dataListArr addObjectsFromArray:dataListArr];
        if (self.dataListArr.count == 0) {
            self.nolab.hidden = NO;
        } else {
            self.nolab.hidden = YES;
        }
        [self.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [self.curTableView sy_endRefreshing];
    }];
    
}


#pragma mark - tabl
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 95;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYTongJiTableViewCell *pgCell = [tableView dequeueReusableCellWithIdentifier:@"SYTongJiTableViewCell" forIndexPath:indexPath];
    
    pgCell.selectionStyle = UITableViewCellSelectionStyleNone;
    pgCell.examModel = self.dataListArr[indexPath.row];
    return pgCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SYExamListModel *listModel = self.dataListArr[indexPath.row];
    
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestStatisticsServiceEXamInfoTemplateId:listModel.templateId success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        SYExamInfoModel *infoModel = nil;
        if (dataListArr.count > 0) {
            infoModel = dataListArr.firstObject;
            infoModel.templateId = listModel.templateId;
        }
        if (self.block) {
            self.block(infoModel);
        }

    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
    
    
    
    
    
}


#pragma mark -


- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
        [_curTableView registerCellWithNIbNames:@[@"SYTongJiTableViewCell"]];
    }
    return _curTableView;
}


@end






















